<?php

require_once("../../connection.php");

require_once("../../article/to/article_to.php");
require_once("../../article/dao/article_dao.php");


class Mysql_article_dao implements Article_dao {
	
	public function create($article_to) {
		try {	
			$link = Connection::singleton()->transaction_begin();
			
			$title = mysql_real_escape_string($article_to->get_title());
			$autor_login = mysql_real_escape_string($article_to->get_autor_login());
			$date = mysql_real_escape_string($article_to->get_date());
			$state = mysql_real_escape_string($article_to->get_state());
			$view = $article_to->get_view();			
			
			$result = mysql_query("insert into article 
									(title, autor_login, date, state, view)
									values ('$title', '$autor_login', 
									'$date', '$state', '$view')", $link);
			
			if (!$result || (mysql_affected_rows($link) != 1) 
					|| (($number = mysql_insert_id($link)) == 0)) 
				throw new Exception(NOT_ROW_INSERTED);
			
												
			Connection::singleton()->transaction_commit($link);
			$article_to = new Article_to($number, $title, $autor_login, $date, $state, $view);
			return $article_to;
			
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
		
		
	public function exist($number){
		try {	
			$link = Connection::singleton()->transaction_begin();

			$result = mysql_query("select * from article where number='$number'", $link);
											
			if (!$result || (mysql_num_rows($result) != 1)) {
				Connection::singleton()->transaction_commit($link);
				return false;
			}
			
			Connection::singleton()->transaction_commit($link);
			return true;
			
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
	
	public function find_by_number_article($number){
		try {	
			$link = Connection::singleton()->transaction_begin();

			$result = mysql_query("select * from article where number='$number'", $link);
											
			if (!$result || (mysql_num_rows($result) != 1)) {
				throw new Exception(NOT_ROW_FOUND);
			}
			
			Connection::singleton()->transaction_commit($link);
			return mysql_fetch_assoc($result);
			
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
		    
	public function update($article_to){
		 try {	
			$link = Connection::singleton()->transaction_begin();

			$number = $article_to->get_number();
			$title = mysql_real_escape_string($article_to->get_title());
			$state = mysql_real_escape_string($article_to->get_state());
			$view = $article_to->get_view();
			
			$result = mysql_query("update article set title='$title',
									state='$state', view='$view'
									where number='$number'", $link);
																	
			if (!$result || (mysql_affected_rows($link) != 1)) 
				throw new Exception(NOT_ROW_UPDATED);
			
			Connection::singleton()->transaction_commit($link);
			
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
    
	public function remove($number){
		 try {	
			$link = Connection::singleton()->transaction_begin();

			$result = mysql_query("delete from article where number='$number'", $link);
																	
			if (!$result || (mysql_affected_rows($link) != 1)) {
				throw new Exception(NOT_ROW_DELETED);
			}
			
			Connection::singleton()->transaction_commit($link);
			
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
	
}
	      

?>